package com.example.demo.contorller;

import com.example.demo.bean.Regional;
import com.example.demo.common.ResponseCode;
import com.example.demo.common.ServerResponse;
import com.example.demo.service.RegionalService;
import lombok.AllArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author ouyangzhixiang
 * @date 2018/6/23
 */
@RequestMapping("/regional/")
@RestController
@AllArgsConstructor
public class RegionalController {
    private final RegionalService regionalService;
    @GetMapping(value = "regional_list")
    public ServerResponse<List<Regional>> regionalList(Integer parentId) {
        return regionalService.list(parentId);
    }

    @GetMapping(value = "select_find_one")

    public ServerResponse selectOne(Integer id){
        return regionalService.findOne(id);
    }

    @PostMapping("add_regional")
    public ServerResponse<Regional> addRegional(@Valid Regional regional, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return regionalService.addRegional(regional);
    }

    @DeleteMapping("delete_regional/{id}")
    public void deleteDepartment(@PathVariable("id") Integer id) {
        regionalService.deleteRegional(id);
    }

    @PutMapping("regional_update")
    public ServerResponse<Regional> regionalUpdate(Regional regional) {
        ServerResponse<Regional> responseRegional = regionalService.regionalUpdate(regional);
        return responseRegional;
    }
}
